package com.ypf.rpc.message;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
public abstract class Message implements Serializable {

    /**
     * 根据消息类型字节，获得对应的消息 class
     * @param messageType 消息类型字节
     * @return 消息 class
     */
    public static Class<? extends Message> getMessageClass(int messageType) {
        return messageClasses.get(messageType);
    }

    private int sequenceId;

    private int messageType;

    public abstract int getMessageType();

    public static final int heartMessage = 0;
    public static final int PingMessage = 1;
    public static final int PongMessage = 2;

    /**
     *
     */
    public static final int  RpcRegisterRequestMessage = 3;
    public static final int  RpcRegisterResponseMessage = 4;

    /**
     *
     */
    public static final int RpcQueryRegisterRequestMessage = 5;
    public static final int RpcQueryRegisterResponseMessage = 6;

    /**
     * 请求类型 byte 值
     */
    public static final int RPC_MESSAGE_TYPE_REQUEST = 101;
    /**
     * 响应类型 byte 值
     */
    public static final int  RPC_MESSAGE_TYPE_RESPONSE = 102;




    private static final Map<Integer, Class<? extends Message>> messageClasses = new HashMap<>();

    static {
        messageClasses.put(RPC_MESSAGE_TYPE_REQUEST, RpcRequestMessage.class);
        messageClasses.put(RPC_MESSAGE_TYPE_RESPONSE, RpcResponseMessage.class);
        messageClasses.put(PingMessage, PingMessage.class);
        messageClasses.put(PongMessage, PongMessage.class);
        messageClasses.put(RpcRegisterRequestMessage,RpcRegisterRequestMessage.class);
        messageClasses.put(RpcRegisterResponseMessage,RpcRegisterResponseMessage.class);

        messageClasses.put(RpcQueryRegisterRequestMessage,RpcQueryRegisterRequestMessage.class);
        messageClasses.put(RpcQueryRegisterResponseMessage,RpcQueryRegisterResponseMessage.class);
    }

}
